using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;

namespace SharpDiary.Core.Controls
{
    [DefaultEvent("Click")]
    public class ImageButton : UserControl
    {
        #region Fields & Properties

        private Image _imageOnMouseEnter;

        public Image ImageOnMouseEnter
        {
            get { return _imageOnMouseEnter; }
            set { _imageOnMouseEnter = value; }
        }

        private Image _imageOnMouseDown;

        public Image ImageOnMouseDown
        {
            get { return _imageOnMouseDown; }
            set { _imageOnMouseDown = value; }
        }

        private Image _imageOnDisable;

        public Image ImageOnDisable
        {
            get { return _imageOnDisable; }
            set
            {
                _imageOnDisable = value;

                if (Enabled)
                {
                    this.BackgroundImage = value;
                }
                else
                {
                    if (_imageOnDisable != null)
                    {
                        this.BackgroundImage = _imageOnDisable;
                    }
                }
            }
        }

        private Image _imageOnNormal;

        public Image ImageOnNormal
        {
            get { return _imageOnNormal; }
            set
            {
                _imageOnNormal = value;

                if (Enabled)
                {
                    this.BackgroundImage = value;
                }
                else
                {
                    if (_imageOnDisable != null)
                    {
                        this.BackgroundImage = _imageOnDisable;
                    }
                }
            }
        }

        #endregion

        #region Constructors

        public ImageButton()
            : base()
        {
            DoubleBuffered = true;
            ResizeRedraw = true;

            this.BackColor = Color.Transparent;
            this.BackgroundImageLayout = ImageLayout.Zoom;
            this.Size = new Size(75, 23);

            if (this.Enabled)
            {
                if (_imageOnNormal != null)
                {
                    this.BackgroundImage = _imageOnNormal;
                }
            }
            else
            {
                if (_imageOnDisable != null)
                {
                    this.BackgroundImage = _imageOnDisable;
                }
            }
        }

        #endregion

        #region Methods

        protected override void OnEnabledChanged(EventArgs e)
        {
            base.OnEnabledChanged(e);

            if (!Enabled)
            {
                if (_imageOnDisable != null)
                {
                    this.BackgroundImage = _imageOnDisable;
                }
                else
                {
                    if (_imageOnNormal != null)
                    {
                        this.BackgroundImage = _imageOnNormal;
                    }
                }
            }
            else
            {
                if (_imageOnNormal != null)
                {
                    this.BackgroundImage = _imageOnNormal;
                }
            }
        }

        protected override void OnMouseEnter(EventArgs e)
        {
            base.OnMouseEnter(e);

            if (!this.Enabled) { return; }

            if (_imageOnMouseEnter != null)
            {
                this.BackgroundImage = _imageOnMouseEnter;
            }
        }

        protected override void OnMouseLeave(EventArgs e)
        {
            base.OnMouseLeave(e);

            if (!this.Enabled) { return; }

            if (_imageOnNormal != null)
            {
                this.BackgroundImage = _imageOnNormal;
            }
        }

        protected override void OnBackgroundImageChanged(EventArgs e)
        {
            base.OnBackgroundImageChanged(e);

            if (_imageOnNormal == null)
            {
                _imageOnNormal = this.BackgroundImage;
            }
        }


        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);

            if (!this.Enabled) { return; }

            if ((e.Button == MouseButtons.Left) && (_imageOnMouseDown != null))
            {
                this.BackgroundImage = _imageOnMouseDown;
            }
        }

        protected override void OnMouseUp(MouseEventArgs e)
        {
            base.OnMouseUp(e);

            if (!this.Enabled) { return; }

            if ((e.Button == MouseButtons.Left) && (_imageOnNormal != null))
            {
                this.BackgroundImage = _imageOnNormal;
            }
        }

        #endregion
    }
}
